#include "Gui.h"

Gui::Gui()
    : screen_surface(NULL)
{
}

Gui::~Gui()
{
    SDL_Quit();
}

int Gui::Init(GameState* initial_screen)
{
    if ( InitSDL() != 1 )
        return -1;
    
    SDL_EnableUNICODE(1);
    
    _game_state.ChangeState(initial_screen);
    
    return 1;
}

void Gui::Render()
{
    ClearSurface();
    
    _game_state.Render(screen_surface);
    
    /*
    forn(j,screen_surface->w)
        Graphics::DrawPixel(screen_surface,j,screen_surface->h/2,Graphics::Color(255,0,0));
    
    forn(i,screen_surface->h)
        Graphics::DrawPixel(screen_surface,screen_surface->w/2,i,Graphics::Color(255,0,0));
    */
    
    SDL_Flip(screen_surface);
}

void Gui::HandleEvents()
{
    SDL_Event event;
    
    while ( SDL_PollEvent( &event ) )
    {
        if ( event.type == SDL_QUIT )
        {
            quit_running=true;
        }
        else if (event.type==SDL_KEYDOWN)
            switch( event.key.keysym.sym )
            {
                case SDLK_UP:
                    break;
                case SDLK_DOWN:
                    break;
                case SDLK_RIGHT:
                    break;
                case SDLK_LEFT:
                    break;
                default:
                    char c = (char)event.key.keysym.unicode;
                    KeyboardEvents::GetInstance()->OnCharacterPressed().Notify(c);
                    break;
            }
        else if (event.type==SDL_KEYUP)
            switch( event.key.keysym.sym )
            {
                case SDLK_UP:
                    break;
                case SDLK_DOWN:
                    break;
                case SDLK_RIGHT:
                    break;
                case SDLK_LEFT:
                    break;
                default:
                    break;
            }
        
        else if( event.type == SDL_MOUSEBUTTONDOWN )
        {
            Point2D pos(event.button.x,event.button.y);
            
            if( event.button.button == SDL_BUTTON_LEFT )
                MouseEvents::GetInstance()->OnMousePressed().Notify(pos,MouseEvents::LEFT_BUTTON);
            
            if( event.button.button == SDL_BUTTON_RIGHT )
                MouseEvents::GetInstance()->OnMousePressed().Notify(pos,MouseEvents::RIGHT_BUTTON);
        }
            
        else if( event.type == SDL_MOUSEBUTTONUP )
        {
            Point2D pos(event.button.x,event.button.y);
            
            if( event.button.button == SDL_BUTTON_LEFT )
                MouseEvents::GetInstance()->OnMouseReleased().Notify(pos,MouseEvents::LEFT_BUTTON);
            
            if( event.button.button == SDL_BUTTON_RIGHT )
                MouseEvents::GetInstance()->OnMouseReleased().Notify(pos,MouseEvents::RIGHT_BUTTON);
        }
    }
}

void Gui::Run()
{
    Timer timer;
    quit_running = false;
    
    while (!quit_running)
    {
        timer.Start();
        
        HandleEvents();
        
        Render();
        
        // regulate framerate
        if( timer.GetTicks() < FR )
            SDL_Delay( FR-timer.GetTicks() );
    }
}

int Gui::InitSDL()
{
    /* Init SDL */

    screen_surface = NULL;
    
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        ApplicationLog::Print("Gui::InitSDL: inicializando SDL",ApplicationLog::FATAL);
        return -1;
    }

    screen_surface = SDL_SetVideoMode( SCREEN_WIDTH , SCREEN_HEIGHT , SCREEN_BPP, SDL_SWSURFACE );
    
    if( !screen_surface )
    {
        ApplicationLog::Print("Gui::InitSDL: inicializando pantalla",ApplicationLog::FATAL);
        return -1;
    }
    
    return 1;
}

void Gui::ClearSurface()
{
    SDL_FillRect( screen_surface, NULL, 0 );
}

